<?php
class OPSafeHaven {
	
	/**
	* -------------------------------------------------------------------------
	*
	* 									Attributes
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	* Id of the safe haven
	*
	* @var int
	*/
	protected $id;

	/**
	* Position of the safe haven
	*
	* @var OPPosition
	*/
	protected $position;
	
	/**
	* Virtual Character of the safe haven
	*
	* @var OPVirtualCharacter
	*/
	protected $virtualCharacter;
	
	/**
	* Owner of the safe haven
	*
	* @var string
	*/
	protected $owner;
	
	/**
	* Number of players in the safe haven
	*
	* @var int
	*/
	protected $playersIn;
	
	/**
	 * -------------------------------------------------------------------------
	 * 
	 * 									Static
	 * 
	 * -------------------------------------------------------------------------
	 */

	/**
	* Static constructor creating a new safe haven with its position and virtual character
	*
	* @param $id
	* @param $position
	* @param $virtualCharacter
	* @return OPSafeHaven
	*/
	public static function newWithIdPositionAndVirtualCharacter($id, $position, $virtualCharacter){
		$newSafeHaven = new OPSafeHaven();
		$newSafeHaven	->setId($id)
						->setPosition($position)
						->setVirtualCharacter($virtualCharacter);
	
		return $newSafeHaven;
	
	}
	
	/**
	* Static constructor creating a new safe haven with its position, virtual character and owner
	*
	* @param $id
	* @param $position
	* @param $virtualCharacter
	* @param $owner
	* @return OPSafeHaven
	*/
	public static function newWithIdPositionVirtualCharacterAndOwner($id, $position, $virtualCharacter, $owner){
		$newSafeHaven = OPSafeHaven::newWithIdPositionAndVirtualCharacter($id, $position, $virtualCharacter);
		$newSafeHaven->setOwner($owner);
		
		return $newSafeHaven;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	*
	* 									Dynamic
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Constructor
	 */
	public function OPSafeHaven() {
		$this->playersIn = 0;
		
	}
	
	
	/**
	* -------------------------------------------------------------------------
	* 									Setters
	* -------------------------------------------------------------------------
	*/
	
	/**
	* Set the id of the safe haven
	*
	* @param $id
	* @return OPSafeHaven
	*/
	public function setId($id) {
		$this->id = $id;
	
		return $this;
	
	}
	
	/**
	* Set the position to the safe haven
	*
	* @param $position
	* @return OPSafeHaven
	*/
	public function setPosition($position) {
		$this->position = $position;
	
		return $this;
	
	}
	
	/**
	* Set the virtual character to the safe haven
	*
	* @param $virtualCharacter
	* @return OPSafeHaven
	*/
	public function setVirtualCharacter($virtualCharacter) {
		$this->virtualCharacter = $virtualCharacter;
	
		return $this;
	
	}
	
	/**
	* Set the number of players in the safe haven
	*
	* @param $playersIn
	* @return OPSafeHaven
	*/
	public function setPlayersIn($playersIn) {
		$this->playersIn = $playersIn;
	
		return $this;
	
	}
	
	/**
	* Set the number of players in the safe haven
	*
	* @param $owner
	* @return OPSafeHaven
	*/
	public function setOwner($owner) {
		$this->owner = $owner;
	
		return $this;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Getters
	* -------------------------------------------------------------------------
	*/
	
	/**
	* Return the id of the safe haven
	*
	* @return int
	*/
	public function id() {
		return $this->id;
	
	}
	
	/**
	* Return the position of the safe haven
	*
	* @return OPSafeHaven
	*/
	public function position() {
		return $this->position;
	
	}
	
	/**
	* Return the virtual character of the safe haven
	*
	* @return OPVirtualCharacter
	*/
	public function virtualCharacter() {
		return $this->virtualCharacter;
	
	}
	
	/**
	* Return the number of players in the safe haven
	*
	* @return int
	*/
	public function playersIn() {
		return $this->playersIn;
	
	}
	
	/**
	* Return the owner of the safe haven
	*
	* @return string
	*/
	public function owner() {
		return $this->owner;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Faction Management
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Allow a faction to capture a safe haven
	 * 
	 * @param $aPlayer
	 */
	public function isCapturedBy($aPlayer) {
		$this->owner = $aPlayer->faction();
		
	}
	
	/**
	 * Return if the safe haven is owned by rebels
	 * 
	 * @return boolean
	 */
	public function isRebelFaction() {
		return OPFaction::REBELS_FACTION == $this->owner();
	
	}
	
	/**
	* Return if the safe haven is owned by oppressors
	*
	* @return boolean
	*/
	public function isOppressorFaction() {
		return OPFaction::OPPRESSORS_FACTION == $this->owner();
	
	}
	
	/**
	 * Change the owner of the safe haven
	 * 
	 * @return OPSafeHaven
	 */
	public function changeOwner() {
		if(OPFaction::OPPRESSORS_FACTION == $this->owner()) {
			$this->setOwner(OPFaction::REBELS_FACTION);
			
		}else {
			$this->setOwner(OPFaction::OPPRESSORS_FACTION);
			
		}
		
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Players Management
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Add a player in the safe haven
	 * 
	 * @param $aPlayer
	 * @return OPSafeHaven
	 */
	public function addPlayer($aPlayer) {
		
		if ($this->owner() != $aPlayer->faction()) {
			return false;
			
		}
		
		$this->playersIn += 1;
		
		return $this;
		
	}
	
	/**
	 * Remove a player from the safe haven
	 * 
	 * @param $aPlayer
	 * @return OPSafeHaven
	 */
	public function removePlayer($aPlayer) {
		$this->playersIn -= 1;
		
		return $this;
		
	}
	

	
}